BinaryReader.Read 方法 (System.IO)

您所在的位置:网站首页 count the 怎么读 BinaryReader.Read 方法 (System.IO)

BinaryReader.Read 方法 (System.IO)

2024-06-17 10:02| 来源: 网络整理| 查看: 265

Source:BinaryReader.cs Source:BinaryReader.cs Source:BinaryReader.cs

从字符数组中的指定点开始,从流中读取指定的字符数。

public: virtual int Read(cli::array ^ buffer, int index, int count); public virtual int Read (char[] buffer, int index, int count); abstract member Read : char[] * int * int -> int override this.Read : char[] * int * int -> int Public Overridable Function Read (buffer As Char(), index As Integer, count As Integer) As Integer 参数 buffer Char[]

将数据读入的缓冲区。

index Int32

缓冲区中的起始点,在该处开始读入缓冲区。

count Int32

要读取的字符数。

返回 Int32

读入缓冲区的总字符数。 如果当前可用的字节没有请求的那么多,此数可能小于所请求的字符数;如果到达了流的末尾,此数可能为零。

例外 ArgumentException

缓冲区长度减去 index 小于 count。

- 或 -

要读取的解码字符数大于 count。 如果 Unicode 解码器返回回退字符或代理项对,则可能发生此情况。

ArgumentNullException

buffer 为 null。

ArgumentOutOfRangeException

index 或 count 为负数。

ObjectDisposedException

流已关闭。

IOException

出现 I/O 错误。

示例

以下示例演示如何使用内存作为后备存储来读取和写入数据。 此示例向控制台显示无效文件路径字符的列表。 尽管代码尝试显示所有无效文件路径字符的列表,但并非所有字符都在可显示的字符集中。 由于无效字符的列表可能因系统而异,因此此代码的输出也可能有所不同。

using namespace System; using namespace System::IO; int main() { array^invalidPathChars = Path::InvalidPathChars; MemoryStream^ memStream = gcnew MemoryStream; BinaryWriter^ binWriter = gcnew BinaryWriter( memStream ); // Write to memory. binWriter->Write( "Invalid file path characters are: " ); binWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length ); // Create the reader using the same MemoryStream // as used with the writer. BinaryReader^ binReader = gcnew BinaryReader( memStream ); // Set Position to the beginning of the stream. binReader->BaseStream->Position = 0; // Read the data from memory and write it to the console. Console::Write( binReader->ReadString() ); int arraySize = (int)(memStream->Length - memStream->Position); array^memoryData = gcnew array(arraySize); binReader->Read( memoryData, 0, arraySize ); Console::WriteLine( memoryData ); } using System; using System.IO; class BinaryRW { static void Main() { char[] invalidPathChars = Path.InvalidPathChars; MemoryStream memStream = new MemoryStream(); BinaryWriter binWriter = new BinaryWriter(memStream); // Write to memory. binWriter.Write("Invalid file path characters are: "); binWriter.Write( Path.InvalidPathChars, 0, Path.InvalidPathChars.Length); // Create the reader using the same MemoryStream // as used with the writer. BinaryReader binReader = new BinaryReader(memStream); // Set Position to the beginning of the stream. memStream.Position = 0; // Read the data from memory and write it to the console. Console.Write(binReader.ReadString()); int arraySize = (int)(memStream.Length - memStream.Position); char[] memoryData = new char[arraySize]; binReader.Read(memoryData, 0, arraySize); Console.WriteLine(memoryData); } } open System.IO let invalidPathChars = Path.GetInvalidPathChars() let memStream = new MemoryStream() let binWriter = new BinaryWriter(memStream) // Write to memory. binWriter.Write "Invalid file path characters are: " binWriter.Write(invalidPathChars, 0, invalidPathChars.Length) // Create the reader using the same MemoryStream // as used with the writer. let binReader = new BinaryReader(memStream) // Set Position to the beginning of the stream. memStream.Position int let memoryData = Array.zeroCreate arraySize binReader.Read(memoryData, 0, arraySize) |> ignore printfn $"{memoryData}" Imports System.IO Public Class BinaryRW Shared Sub Main() Dim invalidPathChars() As Char = Path.InvalidPathChars Dim memStream As new MemoryStream() Dim binWriter As New BinaryWriter(memStream) ' Write to memory. binWriter.Write("Invalid file path characters are: ") binWriter.Write(Path.InvalidPathChars, 0, _ Path.InvalidPathChars.Length) ' Create the reader using the same MemoryStream ' as used with the writer. Dim binReader As New BinaryReader(memStream) ' Set Position to the beginning of the stream. memStream.Position = 0 ' Read the data from memory and write it to the console. Console.Write(binReader.ReadString()) Dim upperBound As Integer = _ CInt(memStream.Length - memStream.Position) - 1 Dim memoryData(upperBound) As Char binReader.Read(memoryData, 0, upperBound) Console.WriteLine(memoryData) End Sub End Class 注解

BinaryReader 读取操作失败后,不会还原文件位置。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。

另请参阅 文件和流 I/O 如何:从文件中读取文本 如何:将文本写入文件 适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3